<!-- NOTE: entity-field.mixin provides the observables passed to cvc-entity-select below -->
<nz-row [nzGutter]="[6, 6]">
  <nz-col
    *ngIf="!props.hideFeatureTypeSelect"
    nzFlex="100px">
    <nz-select
      [disabled]="!props.canChangeFeatureType"
      [(ngModel)]="selectedFeatureType"
    (ngModelChange)="onFeatureType$.next($event); formControl.setValue(undefined)">
      <nz-option
        nzValue="GENE"
        nzLabel="Gene"></nz-option>
      <nz-option
        nzValue="FACTOR"
        nzLabel="Factor"></nz-option>
      <nz-option
        nzValue="FUSION"
        nzLabel="Fusion"></nz-option>
    </nz-select>
  </nz-col>
  <nz-col nzFlex="auto">
    <cvc-entity-select
      [cvcSelectMode]="props.isMultiSelect ? 'multiple' : 'default'"
      [cvcCustomTemplate]="selectedTemplate"
      [cvcFormControl]="formControl"
      [cvcFormlyAttributes]="field"
      [cvcPlaceholder]="props.placeholder"
      [cvcEntityName]="props.entityName"
      [cvcAddEntity]="addFeature"
      [cvcAddEntityBehavior]="showAddBehavior"
      [cvcResults]="result$ | ngrxPush"
      [cvcOptions]="selectOption$ | ngrxPush"
      [cvcShowError]="showError"
      [cvcLoading]="isLoading$ | ngrxPush"
      (cvcOnSearch)="onSearch$.next($event)"
      (cvcOnOpenChange)="onOpenChange$ ? onOpenChange$.next($event) : null"
      (cvcSelectComponent)="selectComponent = $event"
      (cvcOnModelChange)="props.change && props.change(field, $event)">
    </cvc-entity-select>
  </nz-col>
</nz-row>

<!-- select option templates -->
<!-- result$ updates generate an array of #optionTemplates. entity-tag-field.mixin watches #optionTemplates changes, and attaches each to an NzSelectOptionTemplateInterface an object, which are emitted from selectOption$. selectOption$ is passed to <cvc-entity-select> via [cvcOptions] above -->
<ng-container *ngrxLet="onSearch$ as searchString">
  <ng-container *ngFor="let result of result$ | ngrxPush; index as i">
    <ng-template #optionTemplates>
      <cvc-entity-tag
        [cvcDisableLink]="true"
        [cvcCacheId]="result.__typename + ':' + result.id"
        [cvcEmphasize]="searchString"></cvc-entity-tag>
      <span
        nz-typography
        nzType="secondary">
        <ng-container *ngIf="result.featureAliases.length > 0; else noAliases">
          <em>
            <span
              nz-typography
              nzType="secondary"
              nz-tooltip
              [nzTooltipTitle]="result.featureAliases.join(', ')"
              [innerHtml]="
                result.featureAliases.join(', ')
                  | highlightTypeahead : searchString
              "></span
          ></em>
        </ng-container>
        <ng-template #noAliases>--</ng-template>
      </span>
    </ng-template>
  </ng-container>
</ng-container>

<!-- selected option item template -->
<!-- displays entity tag, specifying context and mode depending on
    if field is multi-select or single -->
<ng-template
  #selectedTemplate
  let-selected>
  <div class="ant-select-selection-item-content">
    <cvc-entity-tag
      [cvcCacheId]="'Feature:' + selected.nzValue"
      [cvcContext]="props.isMultiSelect ? 'multi-select-item' : 'select-item'"
      [cvcMode]="props.isMultiSelect ? 'default' : 'closeable'"
      (cvcOnClose)="onTagClose$.next(selected.nzValue)"></cvc-entity-tag>
  </div>
</ng-template>

<ng-template
  #addFeature
  let-searchStr
  let-model="model">
  @if (selectedFeatureType == 'FUSION') {
  <nz-space nzDirection="vertical">
    <span
      *nzSpaceItem
      nz-typography
      >{{searchStr}} does not match any existing Fusions</span
    >
    <button
      *nzSpaceItem
      nz-button
      nzType="primary"
      (click)="createFusionModal()">
      Open Fusion Builder
    </button>
  </nz-space>
  } @else {
  <cvc-feature-quick-add-form
    [cvcSearchString]="searchStr"
    [cvcFeatureType]="onFeatureType$ | ngrxPush"
    (cvcOnCreate)="onSelectOrCreate($event)">
  </cvc-feature-quick-add-form>
  }
</ng-template>
